﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace 扑克牌
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public const int n = 11;
        public int[,] chessbox = new int[n, n];//前x后y

        //收藏黑白棋坐标
        List<int> black_x = new List<int>();
        List<int> white_x = new List<int>();
        List<int> black_y = new List<int>();
        List<int> white_y = new List<int>();

        public Graphics gra;
        public Image picture = Properties.Resources.ditu;
        //当前下子，黑1白2
        public int ing = 1;

        //
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ok(object sender, MouseEventArgs e)
        {
            gra = Graphics.FromImage(picture);
            this.Show();
            int x = tidy(e.X);
            int y = tidy(e.Y);

            if (x == 0 || y == 0)
            {
                return;
            }

            int xx = coord(x);
            int yy = coord(y);
            if (ing == 1)
            {
                if (!ischanges(xx, yy))
                {
                    MessageBox.Show("有子了");
                    return;
                }
                chessbox[xx, yy] = 1;
                gra.DrawImage(Properties.Resources.black, x - 25, y - 25);
                if (iswin(xx, yy, ing))
                {
                    MessageBox.Show("黑棋胜利");
                    birth();
                }       
                pictureBox2.Image = Properties.Resources.white ;
                ing++;
            }
            else if (ing == 2)
            {
                if (!ischanges(xx, yy))
                {
                    MessageBox.Show("有字了");
                    return;
                }
                chessbox[xx, yy] = 2;
                gra.DrawImage(Properties.Resources.white, x - 25, y - 25);
                if (iswin(xx, yy, ing))
                {
                    MessageBox.Show("白棋胜利");
                    birth();
                } 
                pictureBox2.Image = Properties.Resources.black;
                ing--;
            }
            pictureBox1.Image = picture;
            gra.Dispose();
      //      check();

        }

        //初始化
        public void birth()
        {
            button1.Visible = true;
        }
        //核查
      /*  public void check()
        {
            string a = "";
            for (int y = 0; y < n; y++)
            {
                for (int x = 0; x < n; x++)
                {
                    a = a + chessbox[x, y];
                }
                a = a + "\n";
            }
            label3.Text = a;

        }*/

        //坐标的整理
        public int tidy(int x)
        {
            if (x < 30 || x > 540)
            {
                return 0;
            }
            if (x % 50 < 25)
                return x - x % 50;
            else if (x % 50 >= 25)
                return x + (50 - x % 50);
            else
                return 0;
        }

        //坐标点的转换
        public int coord(int x)
        {
            return x / 50 - 1;
        }

        public bool ischanges(int x, int y)
        {
            if (chessbox[x, y] == 1 || chessbox[x, y] == 2)
                return false;
            return true;
        }

        public bool iswin(int x, int y, int chess)
        {
            int num = 0, i;
            //x轴方面
            for (i = 1; i < 6; i++)
            {
                if (x + i > 10) break;
                if (chessbox[x + i, y] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            for (i = 1; i < 6; i++)
            {
                if (x - i < 0) break;
                if (chessbox[x - i, y] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            if (num >= 5)
            {
                return true;
            }

            num = 0;
            //y轴方面
            for (i =1; i < 6; i++)
            {
                if (y + i > 10) break;
                if (chessbox[x, y + i] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            for (i =1; i < 6; i++)
            {
                if (y - i < 0) break;
                if (chessbox[x, y - i] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            if (num >= 5)
            {
                return true;
            }
            num = 0;
            //xy轴方面
            for (i = 0; i < 6; i++)
            {
                if (x + i > 10 || y + i > 10) break;
                if (chessbox[x + i, y + i] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            for (i = 0; i < 6; i++)
            {
                if (x - i < 0 || y - i < 0) break;
                if (chessbox[x - i, y - i] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            if (num >= 5)
            {
                return true;
            }
            num = 0;
            //xy轴方面
            for (i = 0; i < 6; i++)
            {
                if (x - i < 0 || y + i > 10) break;
                if (chessbox[x - i, y + i] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            for (i = 0; i < 6; i++)
            {
                if (x + i > 10 || y - i < 0) break;
                if (chessbox[x + i, y - i] == chess)
                {
                    num++;
                }
                else
                    break;
            }
            if (num >= 5)
            {
                return true;
            }

            return false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Visible = false;
            for (int y = 0; y < n; y++)
            {
                for (int x = 0; x < n; x++)
                {
                    chessbox[x, y] = 0;
                }
            }
            picture = Properties.Resources.ditu;
            ing = 1;
        }
    }
}
